<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tableList">
      <el-table-column label="id" prop="id" width="200" />
      <el-table-column label="手机号" prop="phone" width="160" />
      <el-table-column label="类型" prop="type" width="100">
        <template slot-scope="scope">{{ types[scope.row.type] }}</template>
      </el-table-column>
      <el-table-column label="备注" prop="remark" />
      <el-table-column label="站点ids" prop="siteIds" width="200">
        <template slot-scope="scope">
          <template v-if="scope.row.siteIds">
            <el-tag
              v-for="(item, index) in JSON.parse(scope.row.siteIds)"
              :key="index"
              >{{ siteIdsMap[item] }}</el-tag
            >
          </template>
        </template>
      </el-table-column>

      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="160"
      >
        <template slot-scope="scope" v-if="scope.row.roleId !== 1">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page-sizes="[10, 20, 30, 50, 500, 1000]"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 新增/修改 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <!-- 手机号 -->
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <!-- 类型 -->
        <el-form-item label="类型" prop="type">
          <el-select
            style="width: 100%"
            v-model="form.type"
            placeholder="请选择类型"
          >
            <el-option label="黑名单" value="BLACK"></el-option>
            <el-option label="白名单" value="WHITE"></el-option>
          </el-select>
        </el-form-item>
        <!-- 站点ids -->
        <el-form-item label="站点ids" prop="siteIds">
          <el-select
            style="width: 100%"
            v-model="form.siteIds"
            placeholder="请选择站点"
            multiple
          >
            <el-option
              v-for="(item, index) in siteList"
              :key="index"
              :label="item.siteName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- 备注 -->
        <el-form-item label="备注">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入备注"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getListAPI as getSiteListAPI } from "@/api/charge/site";
import { getListAPI, addOrUpdateAPI, deleteAPI } from "@/api/charge/black";

export default {
  name: "Site",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      tableList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        phone: undefined,
      },
      // 表单参数
      form: {},

      // 表单校验
      rules: {
        phone: [{ required: true, message: "请输入手机号", trigger: "blur" }],
        type: [{ required: true, message: "请选择类型", trigger: "change" }],
      },

      types: {
        BLACK: "黑名单",
        WHITE: "白名单",
      },

      siteList: [],
      siteIdsMap: {},
    };
  },
  created() {
    this.getSiteList();
  },
  methods: {
    // 查询全部站点
    getSiteList() {
      getSiteListAPI({
        pageNum: "1",
        pageSize: "500",
      }).then((res) => {
        this.siteList = res.rows;
        this.siteList.forEach((item) => {
          this.siteIdsMap[item.id] = item.siteName;
        });

        this.getList();
      });
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      getListAPI(this.queryParams).then((res) => {
        this.tableList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加黑/白名单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = {
        id: row.id || "",
        type: row.type || "",
        phone: row.phone || "",
        siteIds: row.siteIds ? JSON.parse(row.siteIds) : "",
        remark: row.remark || "",
      };
      this.open = true;
      this.title = "修改黑/白名单";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          addOrUpdateAPI({
            ...this.form,
            siteIds: this.form.siteIds ? JSON.stringify(this.form.siteIds) : "",
          }).then((res) => {
            if (this.form.id) {
              this.$modal.msgSuccess("修改成功");
            } else {
              this.$modal.msgSuccess("新增成功");
            }
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm("确认删除吗？")
        .then(function () {
          return deleteAPI([row.id]);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
